import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  UseGuards,
  Request,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { TeamService } from './team.service';
import { CreateTeamDto } from './dto/create-team.dto';
import { UpdateTeamDto } from './dto/update-team.dto';
import { AddMemberDto } from './dto/add-member.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@ApiTags('teams')
@ApiBearerAuth('JWT-auth')
@Controller('teams')
@UseGuards(JwtAuthGuard)
export class TeamController {
  constructor(private readonly teamService: TeamService) {}

  @Post()
  @ApiOperation({
    summary: '创建团队',
    description: '用户申请创建新团队，创建者自动成为团队成员',
  })
  @ApiResponse({ status: 201, description: '团队创建成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 401, description: '未授权' })
  create(@Body() createTeamDto: CreateTeamDto, @Request() req) {
    return this.teamService.create(createTeamDto, req.user.id);
  }

  @Get()
  @ApiOperation({
    summary: '获取所有团队',
    description: '获取系统中所有团队列表',
  })
  @ApiResponse({ status: 200, description: '成功获取团队列表' })
  @ApiResponse({ status: 401, description: '未授权' })
  findAll() {
    return this.teamService.findAll();
  }

  @Get('my-teams')
  @ApiOperation({
    summary: '获取我的团队',
    description: '获取当前用户已加入的所有团队',
  })
  @ApiResponse({ status: 200, description: '成功获取团队列表' })
  @ApiResponse({ status: 401, description: '未授权' })
  findMyTeams(@Request() req) {
    return this.teamService.findUserTeams(req.user.id);
  }

  @Get(':id')
  @ApiOperation({
    summary: '获取团队详情',
    description: '根据 ID 获取团队详细信息',
  })
  @ApiParam({ name: 'id', description: '团队 ID', example: 1 })
  @ApiResponse({ status: 200, description: '成功获取团队信息' })
  @ApiResponse({ status: 404, description: '团队不存在' })
  @ApiResponse({ status: 401, description: '未授权' })
  findOne(@Param('id') id: string) {
    return this.teamService.findOne(+id);
  }

  @Patch(':id')
  @ApiOperation({
    summary: '更新团队信息',
    description: '更新团队名称或描述',
  })
  @ApiParam({ name: 'id', description: '团队 ID', example: 1 })
  @ApiResponse({ status: 200, description: '团队更新成功' })
  @ApiResponse({ status: 404, description: '团队不存在' })
  @ApiResponse({ status: 401, description: '未授权' })
  update(@Param('id') id: string, @Body() updateTeamDto: UpdateTeamDto) {
    return this.teamService.update(+id, updateTeamDto);
  }

  @Delete(':id')
  @ApiOperation({
    summary: '删除团队',
    description: '删除指定团队',
  })
  @ApiParam({ name: 'id', description: '团队 ID', example: 1 })
  @ApiResponse({ status: 200, description: '团队删除成功' })
  @ApiResponse({ status: 404, description: '团队不存在' })
  @ApiResponse({ status: 401, description: '未授权' })
  async remove(@Param('id') id: string) {
    await this.teamService.remove(+id);
    return { message: 'Team deleted successfully' };
  }

  @Post(':id/members')
  @ApiOperation({
    summary: '添加团队成员',
    description: '将用户添加到团队中',
  })
  @ApiParam({ name: 'id', description: '团队 ID', example: 1 })
  @ApiResponse({ status: 200, description: '成员添加成功' })
  @ApiResponse({ status: 400, description: '用户已经是团队成员' })
  @ApiResponse({ status: 404, description: '团队或用户不存在' })
  @ApiResponse({ status: 401, description: '未授权' })
  addMember(@Param('id') id: string, @Body() addMemberDto: AddMemberDto) {
    return this.teamService.addMember(+id, addMemberDto.userId);
  }

  @Delete(':id/members/:userId')
  @ApiOperation({
    summary: '移除团队成员',
    description: '将用户从团队中移除',
  })
  @ApiParam({ name: 'id', description: '团队 ID', example: 1 })
  @ApiParam({ name: 'userId', description: '用户 ID', example: 2 })
  @ApiResponse({ status: 200, description: '成员移除成功' })
  @ApiResponse({ status: 400, description: '不能移除团队创建者' })
  @ApiResponse({ status: 404, description: '团队不存在' })
  @ApiResponse({ status: 401, description: '未授权' })
  removeMember(@Param('id') id: string, @Param('userId') userId: string) {
    return this.teamService.removeMember(+id, +userId);
  }
}
